package com.caigou.modules.login.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.countDownCoroutines
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.login.databinding.FragmentForgetBinding
import com.caigou.modules.login.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job

/**
 * @author #Suyghur.
 * Created on 2021/09/22
 */
@AndroidEntryPoint
class ForgetFragment : BaseFragment(), View.OnClickListener {

    private var _binding: FragmentForgetBinding? = null
    private val binding get() = _binding!!
    private val loginViewModel: LoginViewModel by viewModels()
    private var job: Job? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentForgetBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.forgetBtnCode.setOnClickListener(this)
        binding.forgetBtnNext.setOnClickListener(this)
    }

    override fun lazyLoad() {

    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(loginViewModel.smsCodeLiveData, ::handleSmsCode)
        observe(loginViewModel.forgetLiveData, ::handleForgetPwd)
    }

    override fun onBack() {
        binding.forgetEtPhone.setText("")
        binding.forgetEtCode.setText("")
        binding.forgetEtPwd1.setText("")
        binding.forgetEtPwd2.setText("")
        job?.apply {
            cancel()
            job = null
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Logger.d("onDestroyView")
        job?.apply {
            cancel()
            job = null
        }
        _binding = null
    }

    private fun handleSmsCode(status: Resource<String>) {
        when (status) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(status.errorMsg)) {
                    showToast(status.errorMsg)
                } else {
                    showToast("获取验证码异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                showToast("验证码已下发")
                job?.apply {
                    cancel()
                    job = null
                }
                job = countDownCoroutines(60,
                    { second ->
                        with(binding.forgetBtnCode) {
                            isEnabled = false
                            text = "${second}s"
                        }
                    }, {
                        with(binding.forgetBtnCode) {
                            isEnabled = true
                            text = "获取验证码"
                        }
                    })
            }
        }
    }

    private fun handleForgetPwd(status: Resource<String>) {
        when (status) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(status.errorMsg)) {
                    showToast(status.errorMsg)
                } else {
                    showToast("忘记密码失败")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                if (status.data == null) {
                    return
                }
                (requireActivity() as LoginActivity).onBackPressed()
            }
        }
    }

    override fun onClick(v: View?) {
        v?.apply {
            when (id) {
                binding.forgetBtnCode.id -> {
                    val mobile = binding.forgetEtPhone.text.toString()
                    if (TextUtils.isEmpty(mobile)) {
                        showToast("手机号不能为空")
                        return
                    }
                    loginViewModel.getSmsCode(mobile)
                }
                binding.forgetBtnNext.id -> {
                    val mobile = binding.forgetEtPhone.text.toString()
                    val code = binding.forgetEtCode.text.toString()
                    val pwd1 = binding.forgetEtPwd1.text.toString()
                    val pwd2 = binding.forgetEtPwd2.text.toString()

                    if (TextUtils.isEmpty(mobile)) {
                        showToast("手机号不能为空")
                        return
                    }

                    if (TextUtils.isEmpty(code)) {
                        showToast("验证码不能为空")
                        return
                    }

                    if (TextUtils.isEmpty(pwd1)) {
                        showToast("密码不能为空")
                        return
                    }

                    if (TextUtils.isEmpty(pwd2)) {
                        showToast("确认密码不能为空")
                        return
                    }

                    if ((pwd1.length < 6) || (pwd1.length > 16)) {
                        showToast("请输入6-16位长度密码")
                        return
                    }

                    if (pwd1 != pwd2) {
                        showToast("两次输入的密码不一致")
                        return
                    }

                    loginViewModel.forgetPwd(mobile, code.toLong(), pwd1)
                }
            }
        }
    }
}